package com.donica.wamstest.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.donica.wamstest.common.Constant;
import com.donica.wamstest.entity.CommonFindVO;
import com.donica.wamstest.entity.PageResult;
import com.donica.wamstest.entity.PermissionVO;
import com.donica.wamstest.entity.ReturnMessage;
import com.donica.wamstest.entity.RoleVO;
import com.donica.wamstest.entity.UserRoleVO;
import com.donica.wamstest.entity.UserVO;
import com.donica.wamstest.mapper.UserMapper;
import com.donica.wamstest.utils.CommonUtil;
import com.donica.wamstest.utils.EncryptionUtil;
import com.donica.wamstest.utils.JwtUtil;

@Service
public class UserService {
	@Autowired
	UserMapper userMapper;
	
	public ReturnMessage getRoles(){
	     ReturnMessage rtnMsg = new ReturnMessage();
	    	try {
	    
	    		List<RoleVO> result = userMapper.getRoles();
	    		rtnMsg.setResult(result);
	    	}catch (Exception e) {
	    		rtnMsg.setCode(ReturnMessage.FAIL);
	    		rtnMsg.setMsg(e.getMessage());
	    	}

	        return rtnMsg;
   }
	
	
	public ReturnMessage getPermission(UserVO vo){
	     ReturnMessage rtnMsg = new ReturnMessage();
	    	try {
	    		
				List<PermissionVO> permList = userMapper.getUserPermission(vo);
	    		rtnMsg.setResult(permList);
	    	}catch (Exception e) {
	    		rtnMsg.setCode(ReturnMessage.FAIL);
	    		rtnMsg.setMsg(e.getMessage());
	    	}

	        return rtnMsg;
  }
	
	
	public ReturnMessage login(UserVO vo){
	     ReturnMessage rtnMsg = new ReturnMessage();
	    	try {
	    		//
	    		UserVO user = userMapper.getUser(vo);
	    		
	    		String pwd = vo.getPassword();
				pwd = EncryptionUtil.sha256Hex(pwd + Constant.SALT);
				
				if(!user.getPassword().equals(pwd)) {
					throw new Exception("username or password error!");
				}
				
				String token = JwtUtil.createJWT(6000000, user);
	    		
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("token", token);
				
				List<PermissionVO> permList = userMapper.getUserPermission(vo);
				jsonObject.put("permission", permList);
	    	
	    		rtnMsg.setResult(jsonObject);
	    	}catch (Exception e) {
	    		rtnMsg.setCode(ReturnMessage.FAIL);
	    		rtnMsg.setMsg(e.getMessage());
	    	}

	        return rtnMsg;
   }
	
	public long InsertUser(List<UserVO> volist)
	{
		List<UserRoleVO> rolesList = new ArrayList<UserRoleVO>();
		for (UserVO vo : volist) {
			
			//密码处理
			String pwd = vo.getPassword();
			pwd = EncryptionUtil.sha256Hex(pwd + Constant.SALT);
			vo.setPassword(pwd);
			//空字段处理
			vo.initNullItem();
			
			UserRoleVO roleVO = new UserRoleVO();
			roleVO.setId(CommonUtil.get32UUID());
			roleVO.setUser(vo.getId());
			roleVO.setRole(vo.getRole());
			roleVO.setCreator(vo.getCreator());
			rolesList.add(roleVO);
		}
	     userMapper.InsertUser(volist);
	     return userMapper.InsertUserRole(rolesList);
	}
	
	public long UpdateUser(List<UserVO> volist)
	{
		return userMapper.UpdateUser(volist);
	}
	
	public long deleteUser(List<UserVO> volist)
	{
		return userMapper.deleteUser(volist);
	}
	
	public ReturnMessage searchUser(CommonFindVO vo){
	     ReturnMessage rtnMsg = new ReturnMessage();
	    	try {
	    		long total = userMapper.searchUserCount(vo);
	    		List<UserVO> result = userMapper.searchUser(vo);
	    	    for (UserVO userVO : result) {
					userVO.setPassword("*");
				}
	    		PageResult pageResult = new PageResult();
	    		pageResult.setTotalCount(total);
	    		pageResult.setPageSize(vo.getPageSize());
	    		pageResult.setCurrPage(vo.getCurrPage());
	    		pageResult.setResult(result);
	    		rtnMsg.setResult(pageResult);
	    	}catch (Exception e) {
	    		rtnMsg.setCode(ReturnMessage.FAIL);
	    		rtnMsg.setMsg(e.getMessage());
	    	}

	        return rtnMsg;
    }
}
